﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using TanmiaGrp.Basis;
using TanmiaGrp.GeneralLedger.IRepository.Basis;

namespace TanmiaGrp.GeneralLedger.IRepository
{
    public class RepositoryFactory
    {
        private static string DbProviderName;
        private static Assembly DbProviderAssembly;

        private static IGenericRepository repository;
        private static IGenericQueryRepository queryRepository;

        static RepositoryFactory()
        {
            DbProviderName = ConfigManager.Obj.DbProvider;
            DbProviderAssembly = Assembly.Load(DbProviderName);
        }

        public static T Create<T>(string name)
        {
            return (T)DbProviderAssembly.CreateInstance(DbProviderName + "." + name);
        }

        public static IGenericRepository CreateRepository()
        {
            if (repository == null)
                repository = Create<IGenericRepository>("GenericRepository");

            return repository;
        }

        public static IGenericQueryRepository CreateQueryRepository()
        {
            if (queryRepository == null)
                queryRepository = Create<IGenericQueryRepository>("GenericQueryRepository");

            return queryRepository;
        }

        public static IUnitOfWork CreateUnitOfWork()
        {
            return Create<IUnitOfWork>("UnitOfWork");
        }
    }
}
